<?php

namespace app\model;

use think\facade\Db;

class NetworksModel
{
    public static function lists($param): array
    {
        $uid = PartnerUser::where('id',request()->param('uid'))->value('source');
        $where[] = ['net.uid', 'like', $uid.'%'];
        $where[] = [
            'net.partner_id', '=', $param['partner_id'],
        ];
        if (isset($param['name']) && !empty($param['name'])) {
            $where[] = ['net.name', 'like', '%' . $param['name'] . '%'];
        }
        if ($param['business_id'] > 0) {
            $where[] = ['net.business_id', 'find in set', $param['business_id']];
        }
        if (!empty($param['network_ids'])) {
            $where[] = ['net.id', 'IN', $param['network_ids']];
        }
        if (!empty($param['mobile'])) {
            $where[] = ['net.mobile', '=', $param['mobile']];
        }
        if (!empty($param['service_status'])) {
            $where[] = ['net.service_status', '=', $param['service_status']];
        }              
        if (isset($param['company_name']) && !empty($param['company_name'])) {
            $where[] = ['com.name', 'like', '%' . $param['company_name'] . '%'];
        }
        if ($param['is_export'] == 1) {
            $param['limit'] = Db::table('xl_networks')->alias('net')
            ->join('xl_company com', 'net.company_id = com.id', 'left')
            ->where([$where])->count();
        }
        $networklist = Db::table('xl_networks')->alias('net')
            ->join('xl_company com', 'net.company_id = com.id', 'left')
            ->where("net.status", "<", 3)->order('net.id desc')
            ->where([$where])
            ->page($param['page'] ,$param['limit'] )
            ->field('net.*, com.name as company_name')
            ->select()->toArray();

        foreach ($networklist as $k => $v) {
            $names = Db::table('xl_networks_business')->where("status", "<", 3)
                ->where('id', "in", $v['business_id'])->column('name');
            $networklist[$k]['names'] = implode(",", $names);
            $start_time = Db::table('xl_networks_business_time')->where("status", "<", 3)
                ->where('id', "in", $v['business_id'])->column('start_time');
            $estimate = "";
            $starttime = "";
            foreach ($start_time as $k2 => $v2) {
                $explode_time = explode(":", $v2);
                if ($v2 != end($start_time)) {
                    if ($explode_time[1] > 30) {
                        $estimate .= $explode_time[0] . ":" . ($explode_time[1] - 30) . "/";
                    } else {
                        $estimate .= ($explode_time[0] - 1) . ":" . ($explode_time[1] + 30) . "/";
                    }
                    $starttime .= $v2 . "/";
                } else {
                    if ($explode_time[1] > 30) {
                        $estimate .= $explode_time[0] . ":" . ($explode_time[1] - 30);
                    } else {
                        $estimate .= ($explode_time[0] - 1) . ":" . ($explode_time[1] + 30);
                    }
                    $starttime .= $v2;
                }
            }
            $networklist[$k]['starttime'] = $starttime;
            $networklist[$k]['estimate'] = $estimate;
        }
        $count = Db::table('xl_networks')->alias('net')
            ->join('xl_company com', 'net.company_id = com.id', 'left')
            ->where("net.status", "<", 3)
            ->where([$where])->count();
        return ['networklist' => $networklist, 'count' => $count];
    }

    /**
     * 获取所有仓库类型
     * @return void
     */
    public static function line_data($param)
    {
        $where[] = [
            'uid', 'like', $param['source'].'%'
        ];
        $networks = Db::table('xl_networks')
                      ->where("status", "<", 3)
                      ->where([$where])
                      ->field('id,name,longitude,dimension')
                      ->select()
                      ->toArray();
        foreach ($networks as $k => $v) {
            $count = Db::table('xl_parnter_line')->where('status', '=', 1)->where("network_ids", 'find in set', $v['id'])->count();
            if ($count > 0) {
                //已被使用
                $networks[$k]['type'] = 1;
            } else {
                //未被使用
                $networks[$k]['type'] = 2;
            }
        }
        return $networks;
    }

    /**
     * 获取所有网点数据
     * @return void
     */
    public static function networks_data($param)
    {
        error_reporting(E_ERROR);
        $where[] = [
            'id', 'in', $param['network_ids'],
        ];
        $networks = Db::table('xl_networks')->where("status", "<", 3)->where([$where])
            ->field('id,name,longitude,dimension,business_id')
            ->select()->toArray();
        foreach ($networks as $k => $v) {
            $condition[] = ['id', 'in', $v['business_id']];
            //根据网点获取业务时间
            $networksBussinessTime = Db::table('xl_networks_business_time')->where([$condition])
                ->field('id,end_time')->order('end_time desc ')
                ->find();
            $networks[$k]['end_time'] = $networksBussinessTime['end_time'];
        }
        return $networks;
    }

    /**
     * 获取所有仓库类型
     * @return void
     */
    public static function GetAllWarehouseType()
    {
        return Db::table('xl_warehouse_type')->where("status", "=", 1)
            ->select();
    }

    /**
     * 获取所有业务类型
     * @return void
     */
    public static function GetAllBussineType()
    {
        return Db::table('xl_business_type')->where("status", "=", 1)
            ->select();
    }

    /**
     * 新增网点
     * @return void
     */
    public static function add($data)
    {
        return Db::table('xl_networks')->insert($data);
    }

    /**
     * 新增所有仓库
     * @return void
     */
    public static function edit($data, $id)
    {
        return Db::table('xl_networks')->where('id', '=', $id)->update($data);
    }

    /**
     * 删除仓库
     * @return void
     */
    public static function del($id)
    {
        return Db::table('xl_networks')->where('id', '=', $id)->update(['status' => 3]);
    }

    public static function info($id, $isType = 0)
    {
        $info = Db::table('xl_networks')->where('id', '=', $id)->find();
        if ($isType == 1) {
            $info['type'] = 1;
        } elseif ($isType == 2) {
            $info['type'] = 2;
        }
        return $info;
    }

    public static function escape_service_status($value)
    {
        switch ($value) {
            case 1: $text = '正常'; break;
            default: $text = '停止'; break;
        }
        return $text;
    }
}
